package com.github.maojx0630.translate;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 毛家兴
 * @since 2024-02-04 11:48
 */
@ConfigurationProperties(prefix = "mybatis-translate")
public class TranslateConfig {

    /**
     * 缓存类型
     */
    private CacheType cacheType = CacheType.TIMED;

    /**
     * 容量(0表示无大小限制)
     */
    private int cacheCapacity = 0;

    /**
     * 过期时间( 0 表示无限制，单位毫秒)
     */
    private long cacheTime = 1000 * 5;


    /**
     * 字典查询配置
     */
    private DictConfig dictConfig = new DictConfig();

    /**
     * 表查询配置
     */
    private TableConfig tableConfig = new TableConfig();


    public CacheType getCacheType() {
        return cacheType;
    }

    public void setCacheType(CacheType cacheType) {
        this.cacheType = cacheType;
    }

    public int getCacheCapacity() {
        return cacheCapacity;
    }

    public void setCacheCapacity(int cacheCapacity) {
        this.cacheCapacity = cacheCapacity;
    }

    public long getCacheTime() {
        return cacheTime;
    }

    public void setCacheTime(long cacheTime) {
        this.cacheTime = cacheTime;
    }

    public DictConfig getDictConfig() {
        return dictConfig;
    }

    public void setDictConfig(DictConfig dictConfig) {
        this.dictConfig = dictConfig;
    }

    public TableConfig getTableConfig() {
        return tableConfig;
    }

    public void setTableConfig(TableConfig tableConfig) {
        this.tableConfig = tableConfig;
    }

    public enum CacheType {
        /**
         * 无缓存
         */
        NOT,
        /**
         * 最少使用率缓存(容量,过期时间有效)
         */
        LFU,
        /**
         * 最近最久未使用缓存(容量,过期时间有效)
         */
        LRU,
        /**
         * 定时缓存(过期时间有效)
         */
        TIMED,
    }

    public enum AliasType {
        /**
         * 字段本身
         */
        SELF,
        /**
         * 字段后缀
         */
        SUFFIX,
        /**
         * 字段前缀
         */
        PREFIX,
        /**
         * 其他
         */
        OTHER
    }

    public static class AliasConfig {

        /**
         * 别名值
         */
        private String aliasValue;

        /**
         * 别名类型
         */
        private AliasType aliasType;

        public AliasConfig() {
        }

        public AliasConfig(String aliasValue, AliasType aliasType) {
            this.aliasValue = aliasValue;
            this.aliasType = aliasType;
        }

        public String getAliasValue() {
            return aliasValue;
        }

        public void setAliasValue(String aliasValue) {
            this.aliasValue = aliasValue;
        }

        public AliasType getAliasType() {
            return aliasType;
        }

        public void setAliasType(AliasType aliasType) {
            this.aliasType = aliasType;
        }
    }

    public static class TableConfig {

        /**
         * 开启sql日志
         */
        private boolean enableSqlLog = false;
        /**
         * 忽略表名大小写匹配其他表
         */
        private boolean tableIgnoreCase = true;

        /**
         * 别名默认配置
         */
        private AliasConfig aliasConfig = new AliasConfig("", AliasType.SELF);

        /**
         * 其他表默认别名配置
         */
        private Map<String, AliasConfig> otherAliasConfig = new HashMap<>();

        /**
         * 默认最后执行sql
         */
        private String defaultLastSql = "";

        /**
         * 其他表默认执行sql
         */
        private Map<String, String> otherLastSql = new HashMap<>();

        public boolean isEnableSqlLog() {
            return enableSqlLog;
        }

        public void setEnableSqlLog(boolean enableSqlLog) {
            this.enableSqlLog = enableSqlLog;
        }

        public boolean isTableIgnoreCase() {
            return tableIgnoreCase;
        }

        public void setTableIgnoreCase(boolean tableIgnoreCase) {
            this.tableIgnoreCase = tableIgnoreCase;
        }

        public AliasConfig getAliasConfig() {
            return aliasConfig;
        }

        public void setAliasConfig(AliasConfig aliasConfig) {
            this.aliasConfig = aliasConfig;
        }

        public Map<String, AliasConfig> getOtherAliasConfig() {
            return otherAliasConfig;
        }

        public void setOtherAliasConfig(Map<String, AliasConfig> otherAliasConfig) {
            this.otherAliasConfig = otherAliasConfig;
        }

        public String getDefaultLastSql() {
            return defaultLastSql;
        }

        public void setDefaultLastSql(String defaultLastSql) {
            this.defaultLastSql = defaultLastSql;
        }

        public Map<String, String> getOtherLastSql() {
            return otherLastSql;
        }

        public void setOtherLastSql(Map<String, String> otherLastSql) {
            this.otherLastSql = otherLastSql;
        }
    }

    public static class DictConfig {

        /**
         * 开启sql日志
         */
        private boolean enableSqlLog = false;
        /**
         * 字典类型字段名
         */
        private String type = "`TYPE`";

        /**
         * 字典标签字段名
         */
        private String label = "`LABEL`";

        /**
         * 字典值字段名
         */
        private String value = "`VALUE`";

        /**
         * 字典表名
         */
        private String table = "sys_dict";

        /**
         * 别名默认配置
         */
        private AliasConfig aliasConfig = new AliasConfig("Label", AliasType.SUFFIX);

        /**
         * 字典表最后执行sql
         */
        private String lastSql = "";

        public boolean isEnableSqlLog() {
            return enableSqlLog;
        }

        public void setEnableSqlLog(boolean enableSqlLog) {
            this.enableSqlLog = enableSqlLog;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getLabel() {
            return label;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getTable() {
            return table;
        }

        public void setTable(String table) {
            this.table = table;
        }

        public AliasConfig getAliasConfig() {
            return aliasConfig;
        }

        public void setAliasConfig(AliasConfig aliasConfig) {
            this.aliasConfig = aliasConfig;
        }

        public String getLastSql() {
            return lastSql;
        }

        public void setLastSql(String lastSql) {
            this.lastSql = lastSql;
        }
    }

}
